package invitator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * Hello world!
 *
 */
public class App {

    public static void main(String[] args) throws ScriptException {
        if (validArgs(args)) return;
        
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        FileReader fileReader = null;
        
        fileReader = receiveFileReader(fileReader, args);
        engine.eval(fileReader);
        
       

    }

    private static boolean validArgs(String[] args) {
        if (args.length == 0) {
            System.out.println("You have to type in path to js file.");
            return true;
        }
        if (args[0].length() == 0) {
            System.out.println("Your path to js file is empty");
            return true;
        }
        File file = new File(args[0]);
        if (!file.exists()) {
            System.out.println("Your path to file " + args[0] + " is wrong! \n File doesn't exist");
            return true;
        }
        return false;
    }

    private static FileReader receiveFileReader(FileReader fileReader, String[] args) {
        try {
            fileReader = new FileReader(new File(args[0]));
       } catch (FileNotFoundException ex) {
           Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
       }return fileReader;
    }
}
